//
//  HSDrawerController.swift
//  Home_school_晓林
//
//  Created by 张晓林 on 16/9/7.
//  Copyright © 2016年 张晓林. All rights reserved.
//

import UIKit

class HSDrawerController: UIViewController {
    /// 获得左边视图控制器
    fileprivate let leftVc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "HSLeftViewController")
    /// 创建标签视图控制器
    fileprivate let tabBarVc = HSTabBarController()
    /// 抽屉效果的动画时长
    fileprivate let animationTime: TimeInterval = 0.5
    /// 记录手指前一次的移动点
    fileprivate var oldPoint: CGPoint = CGPoint.zero
    /// 记录滑动方向
    fileprivate var isRightMove: Bool = true
    /// 懒加载遮盖视图
    fileprivate lazy var coverView: UIButton = UIButton(coverView: 0.0)
    
    /// 左边视图的宽度
    fileprivate var leftViewW: CGFloat {
        return UIScreen.xl_ScreenW() * 0.846
    }
    /// 左边视图动画偏移的最大距离
    fileprivate var leftViewOffset: CGFloat {
        return UIScreen.xl_ScreenW()/4
    }
    /// 计算两个视图的偏移比例
    fileprivate var offsetScale: CGFloat {
        return leftViewOffset / leftViewW
    }
    

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white
        // 添加子视图
        addChildVc()
        
        // 添加手势
        let panGesture = UIPanGestureRecognizer(target: self, action: #selector(panGestrue))
        view.addGestureRecognizer(panGesture)
    }

    func addChildVc() {
        // 添加左边视图
        addChildViewController(leftVc)
        view.addSubview(leftVc.view)
        leftVc.view.frame = CGRect(x: 0, y: 0, width: leftViewW-5, height: UIScreen.xl_screenH())
        // 设置边界阴影
        leftVc.view.layer.shadowColor = UIColor.gray.cgColor
        leftVc.view.layer.shadowOffset = CGSize(width: 3, height: 0)
        leftVc.view.layer.shadowOpacity = 0.9
        // 设置默认偏移
        leftVc.view.transform = CGAffineTransform(translationX: -leftViewOffset, y: 0)
        
        // 添加右边视图
        addChildViewController(tabBarVc)
        view.addSubview(tabBarVc.view)
        /// 使用闭包回调左边按钮的点击事件
        tabBarVc.leftBtnBlock = {[weak self] in
            print("点击了左边按钮")
            self?.clickedNavBtn()
        }
    }
}

// MARK: - 抽屉效果相关
extension HSDrawerController {
    
    /// 手势回调方法
    @objc fileprivate func panGestrue(_ Gesture: UIPanGestureRecognizer) {
        if Gesture.state == .began {
            // 获得第一次触摸点
            oldPoint = Gesture.location(in: Gesture.view)
        }else if Gesture.state == .changed {
            // 获得当前点
            let currentP = Gesture.location(in: Gesture.view)
            // 判断并记录滑动方向
            if oldPoint.x < currentP.x {
                isRightMove = true
            }else {
                isRightMove = false
            }
            
            // 获得手势偏移量
            let offset = Gesture .translation(in: Gesture.view)
            // 获得当前视图的x方向形变量
            let tx = tabBarVc.view.transform.tx + offset.x
            
            // 如果是右滑并且滑动距离已经大于等于左边视图宽度，就停止滑动
            if isRightMove && tx >= leftViewW {
                rightAnimation()
                return
            }
            // 如果是左滑并且滑动距离已经小于等于0，就停止滑动
            if !isRightMove && tx <= 0 {
                leftAnimation()
                return
            }
            
            // 重新设置标签视图的形变属性
            tabBarVc.view.transform = tabBarVc.view.transform.translatedBy(x: offset.x, y: 0)
            leftVc.view.transform = leftVc.view.transform.translatedBy(x: offset.x * offsetScale, y: 0)
            // 重置手势偏移量
            Gesture.setTranslation(CGPoint.zero, in: Gesture.view)
        }else if Gesture.state == .ended {
            // 当手势结束时判断方向，然后根据方向自动完成偏移
            if isRightMove {
                rightAnimation()
            }else {
                leftAnimation()
            }
        }
    }
    
    /// 点击导航按钮的动画方法
    @objc fileprivate func clickedNavBtn() {
        // 根据当前形变属性来判断是打开还是关闭
        if tabBarVc.view.transform.tx == 0 {
            rightAnimation()
        }else {
            leftAnimation()
        }
    }
    
    /// 右滑
    fileprivate func rightAnimation() {
        // 添加遮盖
        tabBarVc.view.addSubview(coverView)
        coverView.addTarget(self, action: #selector(clickedNavBtn), for: .touchUpInside)
        // 动画偏移视图
        UIView.animate(withDuration: animationTime, animations: {[weak self] in
            self?.tabBarVc.view.transform = CGAffineTransform(translationX: self!.leftViewW, y: 0)
            self?.leftVc.view.transform = CGAffineTransform.identity
            self?.coverView.alpha = 0.5
        })
    }
    
    /// 左滑
    fileprivate func leftAnimation() {
        UIView.animate(withDuration: animationTime, animations: {[weak self] in
            self?.tabBarVc.view.transform = CGAffineTransform.identity
            self?.leftVc.view.transform = CGAffineTransform(translationX: -self!.leftViewOffset, y: 0)
            }, completion: {[weak self] (_) in
                // 移除遮盖
                self?.coverView.removeFromSuperview()
                self?.coverView.alpha = 0.0
        }) 
    }
}








